package com.chapp.lastFmWs.parser;

import org.xml.sax.Attributes;

import android.sax.Element;
import android.sax.EndTextElementListener;
import android.sax.TextElementListener;

import com.chapp.base.Location;
import com.chapp.base.Venue;

public class UtilParser {
	protected final static String geoNamesace = "http://www.w3.org/2003/01/geo/wgs84_pos#";

	public static void venueListener(Element elementVenue, final Venue venue,
			final Location location) {

		// <id>8816229</id>
		elementVenue.getChild("id").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						venue.setId(body);
					}
				});

		// <name>Arena</name>
		elementVenue.getChild("name").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						venue.setName(body);
					}
				});

		// <website>http://www.last.fm/venue/8816229</website>
		elementVenue.getChild("website").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						venue.setUrl(body);
					}
				});

		// <phonenumber>+43-(0)1-7988595</phonenumber>
		elementVenue.getChild("phonenumber").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						venue.setPhonenumber(body);
					}
				});

		// <image
		// size="medium">http://userserve-ak.last.fm/serve/64/33623311.png</image>
		elementVenue.getChild("image").setTextElementListener(
				new TextElementListener() {
					String size;;

					@Override
					public void end(String body) {
						if (size.equals("large") && body != null
								&& !body.equals(""))
							venue.setThumbnailURL(body);
					}

					@Override
					public void start(Attributes attributes) {
						size = attributes.getValue(0);
					}
				});

		Element elementLocation = elementVenue.getChild("location");
		// <location>
		// <city>Exeter</city>
		elementLocation.getChild("city").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						location.setCity(body);
					}
				});

		// <country>United Kingdom</country>
		elementLocation.getChild("country").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						location.setCountry(body);
					}
				});

		// <street/>
		elementLocation.getChild("street").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						location.setStreet(body);
					}
				});
		// <postalcode/>
		elementLocation.getChild("postalcode").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						location.setPostalCode(body);
					}
				});

		Element elementGeo = elementLocation.requireChild(geoNamesace, "point");

		// <geo:lat>50.7</geo:lat>
		elementGeo.getChild(geoNamesace, "lat").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						location.setGeoLat(body);
					}
				});
		// <geo:long>-3.5333333</geo:long>
		elementGeo.getChild(geoNamesace, "long").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						location.setGeoLong(body);
					}
				});
	}

}
